<?php


namespace App\Admin\Handlers;

//材料库存处理类
class MaterialHandle
{
    /**
     * 材料库存最大计算
     * @return string
     */
    public function minimun_stock(array $stock, array $need)
    {
        $minimun=[];
        if(is_array($stock) && is_array($need)){
            foreach ($stock as $key => $value){
                $minimun[]=intval($value/$need[$key]);
            }
        }
        if(count($minimun) > 0){
            return min($minimun);
        }
        return false;
    }

    /**
     * 库存扣减算法
     * @return string
     */
    public static function deduction_stock(array $stock, int $need)
    {
        $minimun=[];
        if(is_array($stock) && is_int($need)){
            foreach ($stock as $key => $value){
                $minimun[$key]=$value;
                if($value['status'] == 1){
                    if((int)$value['stock'] >= (int)$need){
                        $minimun[$key]['stock']=(int)$value['stock'] - (int)$need;
                        $minimun[$key]['sell']=(int)$value['sell'] + (int)$need;
                        if((int)$value['stock'] == (int)$need){
                            $minimun[$key]['status']=2;
                        }
                        break;
                    }else{
                        $minimun[$key]['stock']=0;
                        $minimun[$key]['sell']=(int)$value['sell'] + (int)$value['stock'];
                        $minimun[$key]['status']=2;
                        $need=(int)$need-(int)$value['stock'];
                        continue;
                    }
                }
            }
        }
        return $minimun;
    }
}
